package com.ruoyi.web.controller.stock;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.stock.domain.WarehouseOut;
import com.ruoyi.stock.domain.vo.WarehouseOutVo;
import com.ruoyi.stock.service.IWarehouseOutService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 出库Controller
 *
 * @author YwaiX
 * @date 2024-10-29
 */
@RestController
@RequestMapping("/stock/out")
public class WarehouseOutController extends BaseController {
    @Autowired
    private IWarehouseOutService warehouseOutService;

    /**
     * 查询出库列表
     */
    @PreAuthorize("@ss.hasPermi('stock:out:list')")
    @GetMapping("/list")
    public TableDataInfo list(WarehouseOutVo warehouseOut) {
        startPage();
        List<WarehouseOutVo> list = warehouseOutService.selectWarehouseOuts(warehouseOut);
        return getDataTable(list);
    }

    /**
     * 导出出库列表
     */
    @PreAuthorize("@ss.hasPermi('stock:out:export')")
    @Log(title = "出库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WarehouseOut warehouseOut) {
        List<WarehouseOut> list = warehouseOutService.selectWarehouseOutList(warehouseOut);
        ExcelUtil<WarehouseOut> util = new ExcelUtil<WarehouseOut>(WarehouseOut.class);
        util.exportExcel(response, list, "出库数据");
    }

    /**
     * 获取出库详细信息
     */
    @PreAuthorize("@ss.hasPermi('stock:out:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(warehouseOutService.selectWarehouseOutById(id));
    }

    /**
     * 新增出库
     */
    @PreAuthorize("@ss.hasPermi('stock:out:add')")
    @Log(title = "出库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WarehouseOut warehouseOut) {
        return toAjax(warehouseOutService.insertWarehouseOut(warehouseOut));
    }

    /**
     * 修改出库
     */
    @PreAuthorize("@ss.hasPermi('stock:out:edit')")
    @Log(title = "出库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WarehouseOut warehouseOut) {
        return toAjax(warehouseOutService.updateWarehouseOut(warehouseOut));
    }

    /**
     * 删除出库
     */
    @PreAuthorize("@ss.hasPermi('stock:out:remove')")
    @Log(title = "出库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(warehouseOutService.deleteWarehouseOutByIds(ids));
    }

    /**
     * 修改出库
     */
    @PreAuthorize("@ss.hasPermi('stock:out:edit')")
    @Log(title = "出库", businessType = BusinessType.UPDATE)
    @PutMapping("/updateStatus")
    public AjaxResult updateStatus(@RequestBody WarehouseOut warehouseOut) {
        return toAjax(warehouseOutService.updateWarehouseOut(warehouseOut));
    }
}
